package com._119yn.host.gst200.database.dao.impl;

import com._119yn.host.gst200.database.dao.PointTableRecordDao;
import com._119yn.host.gst200.database.entity.PointTableRecord;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class PointTableRecordDaoImpl implements PointTableRecordDao {


    private final JdbcTemplate jdbcTemplate;

    private final NamedParameterJdbcTemplate namedParameterJdbcTemplate; // 推荐使用

    @Override
    public long insert(PointTableRecord pointTableRecord) {
        String sql = "insert into POINT_TABLE_RECORD (DEVICE_ID, DEVICE_ADDRESS, DEVICE_TYPE, PANEL, LOOP, CODE, TEXT, SN) values (?, ?, ?, ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(
                sql,
                pointTableRecord.getDeviceId(),
                pointTableRecord.getDeviceAddress(),
                pointTableRecord.getDeviceType(),
                pointTableRecord.getPanel(),
                pointTableRecord.getLoop(),
                pointTableRecord.getCode(),
                pointTableRecord.getText(),
                pointTableRecord.getSn()
        );
    }

    @Override
    public long insertBatch(List<PointTableRecord> pointTableRecords) {
        String sql = "insert into POINT_TABLE_RECORD (DEVICE_ID, DEVICE_ADDRESS, DEVICE_TYPE, PANEL, LOOP, CODE, TEXT, SN) values (?, ?, ?, ?, ?, ?, ?, ?)";
        int[][] ints = jdbcTemplate.batchUpdate(
                sql,
                pointTableRecords,
                10,
                (PreparedStatement ps, PointTableRecord pointTableRecord) -> {
                    ps.setString(1, pointTableRecord.getDeviceId());
                    ps.setString(2, pointTableRecord.getDeviceAddress());
                    ps.setString(3, pointTableRecord.getDeviceType());
                    ps.setInt(4, pointTableRecord.getPanel());
                    ps.setInt(5, pointTableRecord.getLoop());
                    ps.setInt(6, pointTableRecord.getCode());
                    ps.setString(7, pointTableRecord.getText());
                    ps.setInt(8, pointTableRecord.getSn());
                });
        return Arrays.stream(ints).flatMapToInt(Arrays::stream).count();
    }

    @Override
    public PointTableRecord selectByDeviceType(String deviceType) {
        String sql = "SELECT * FROM POINT_TABLE_RECORD WHERE DEVICE_TYPE = ? ORDER BY SN DESC LIMIT 1";
        List<PointTableRecord> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PointTableRecord.class), deviceType);
        if (list.isEmpty()) {
            return null;
        }
        return list.getFirst();
    }

    @Override
    public List<PointTableRecord> selectByDeviceTypeIn(Set<String> deviceTypes) {
        String sql = "SELECT * FROM POINT_TABLE_RECORD WHERE DEVICE_TYPE in (:deviceTypes)";
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("deviceTypes", deviceTypes);
        return namedParameterJdbcTemplate.query(sql, parameters, new BeanPropertyRowMapper<>(PointTableRecord.class));
    }

    @Override
    public List<PointTableRecord> selectByPanelIn(Collection<Integer> panels) {
        String sql = "SELECT * FROM POINT_TABLE_RECORD WHERE PANEL in (:panel)";
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("panel", panels);
        return namedParameterJdbcTemplate.query(sql, parameters, new BeanPropertyRowMapper<>(PointTableRecord.class));
    }

    @Override
    public long deleteByDeviceId(String deviceId) {
        String sql = "DELETE FROM POINT_TABLE_RECORD WHERE DEVICE_ID = ?";
        return jdbcTemplate.update(sql, deviceId);
    }

    @Override
    public long updateByDeviceId(PointTableRecord pointTableRecord) {
        String sql = "UPDATE POINT_TABLE_RECORD SET DEVICE_ADDRESS=?, DEVICE_TYPE=?, PANEL=?, LOOP=?, CODE=?, TEXT=?,SN=? WHERE DEVICE_ID = ?";
        return jdbcTemplate.update(
                sql,
                pointTableRecord.getDeviceAddress(),
                pointTableRecord.getDeviceType(),
                pointTableRecord.getPanel(),
                pointTableRecord.getLoop(),
                pointTableRecord.getCode(),
                pointTableRecord.getText(),
                pointTableRecord.getSn(),
                pointTableRecord.getDeviceId()
        );
    }

    @Override
    public PointTableRecord selectByDeviceId(String deviceId) {
        String sql = "SELECT * FROM POINT_TABLE_RECORD WHERE DEVICE_ID = ?";
        List<PointTableRecord> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PointTableRecord.class), deviceId);
        if (list.isEmpty()) {
            return null;
        }
        return list.getFirst();
    }

    @Override
    public List<PointTableRecord> selectAll() {
        String sql = "SELECT * FROM POINT_TABLE_RECORD";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PointTableRecord.class));
    }

    @Override
    public PointTableRecord selectByPanelAndDeviceAddressAndDeviceType(int panel, String deviceAddress, String deviceType) {
        String sql = "SELECT * FROM POINT_TABLE_RECORD WHERE PANEL = ? AND DEVICE_ADDRESS = ? AND DEVICE_TYPE = ?";
        List<PointTableRecord> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(PointTableRecord.class), panel, deviceAddress, deviceType);
        if (list.isEmpty()) {
            return null;
        }
        return list.getFirst();
    }

    @Override
    public long truncate() {
        String sql = "TRUNCATE TABLE POINT_TABLE_RECORD";
        return jdbcTemplate.update(sql);
    }

}
